home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk72 / square / squares.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  2KB  |  57 lines

  1. /*  Squares.c  -   Last Edited: 05/8/89       Author: Michael S. Fransyshen
  2.  
  3.     This program is an example of HOW to pass arguments to a C program
  4.     from the CLI environment.  It accepts two numbers (ints) and outputs
  5.     the squares of the numbers in the specified range.  The program's real
  6.     purpose is solely demonstrative, not in its utility.  Notice that 
  7.     a large majority of the program is in the error handling for user 
  8.     input.                              */
  9.  
  10. Bad_Input()      /* This function is called when user input does not meet
  11.                     the required criteria                              */
  12. {
  13.  
  14.    printf("\nSpecify LEGAL START and END values for the output range!\n");
  15.    printf("USAGE:  Squares <Start> <End>\n");
  16.    printf("Where START and END fall within the range: -32768 to +32767\n\n");
  17.    exit();
  18. }
  19.  
  20. main(argc, argv)
  21. int  argc;
  22. char *argv[];
  23. {
  24.  long   Square,Current,End,Error,Start,Temp;
  25.  long   atol();
  26.  
  27.  if( argc != 3 )( Bad_Input() ); /* Check for correct number of arguments */
  28.  
  29.  Start = atol(argv[1]);
  30.  End = atol(argv[2]);
  31.  
  32.  if(Start > 32767 || Start < -32678) ( Bad_Input() );
  33.  if(End > 32767 || End < -32678) ( Bad_Input() );
  34.  
  35.  /* Since atol() will return a 0 value if converting letters, we must check
  36.     to see if the user typed a 0 as an argument, or a string of other ASCII
  37.     characters at the CLI.  In the line below, I test for the ACSII value of
  38.     0 (Zero) pointed to by *argv[1] or *argv[2].  A more thorough explanation
  39.     may be found in the accompanying Square.doc file  */
  40.  
  41.  if(*argv[1] != 48 && Start == 0L || *argv[2] != 48 && End == 0L)
  42.    ( Bad_Input() );
  43.  
  44.  if(Start > End)
  45.   {
  46.     Temp = Start;      /* Control for input where Start > End.  */
  47.     Start = End;       /* Swap the values, Display output from  */
  48.     End = Temp;        /* lowest to highest.                    */
  49.   }
  50.  
  51.  for(Current = Start; Current <= End; Current++)
  52.   {
  53.     Square = Current * Current;
  54.     printf("Current Value: %ld \t  Squared Value: %ld\n",Current,Square);
  55.   }
  56. }
  57.